# -*- coding:utf-8 -*-

import models, service
from .ServiceException import ServiceException

def isAdmin(user):
    return user.type == models.User.TYPE_ADMIN

def hasOverviewRight(user):
    if user.type == models.User.TYPE_ADMIN:
        return True
    company = models.Company.getById(user.company_id)
    if company and (company.type == models.Company.TYPE_ASSESS or company.type == models.Company.TYPE_INSURANCE):
        return True
    return False

def canOperate(user, branchCompanyId):
    can = True
    if user.type == models.User.TYPE_ADMIN:
        can = False
    elif user.type == models.User.TYPE_CHIEF and user.company_id != branchCompanyId:
        can = False
    elif user.type == models.User.TYPE_COMMON:
        company = models.Company.getById(user.company_id)
        if company.type == models.Company.TYPE_BRANCH and user.company_id != branchCompanyId:
            can = False
    if not can:
        raise ServiceException(service.UNAUTHORIZED, '没有权限')

